#! /usr/bin/env python

#
# Author: Gou Pengfei
#

import sys
import os
import optparse
import datetime
import re
from subprocess import call

sys.path.append(os.path.join(os.environ['HOME'],
                             'M5-EDGE', 'configs','common'))
from cpu2000 import *
import cpu2000
        
#benchmarks_to_run = ['crafty']
#benchmarks_to_run = ['gzip_graphic','mcf',
#                     'crafty','parser_spec','vortex_2',
#                     'bzip2_program','twolf','wupwise','swim',
#                     'mgrid','applu','mesa','art_110',
#                     'apsi']

#benchmarks_to_run = ['apsi']

benchmarks_to_run = ['gzip_graphic','vpr_route','gcc_166','mcf',
                     'perlbmk_makerand',
                     'crafty','parser_spec','gap','vortex_2',
                     'bzip2_program','twolf',
                     'wupwise','swim',
                     'mgrid','applu','mesa','art_110',
                     'equake','ammp','apsi']

#benchmarks_to_run = ['gzip_graphic','vpr_route','gcc_166','mcf',
#                     'perlbmk_makerand',
#                     'crafty','parser_spec','gap','vortex_2',
#                     'bzip2_program','twolf']

progname = os.path.basename(sys.argv[0])

output_base_dir = os.path.join(cpu2000.simout_dir, 'trips', 'linux')

optparser = optparse.OptionParser()

optparser.add_option('-v', '--variant', dest='variant',
                     default='fast',
                     help='build variants to test '
                     " (default: '%default')" )
                     
optparser.add_option('-i', '--inputset', dest='input_set',
                     default='test',
                     help='CPU2000 input sets.' )
                     
optparser.add_option('-c', '--conf', dest='configuration',
                     default='simple-valid',
                     help='Configuration of this run' )
                     
(options,leftovers) = optparser.parse_args()

cmd = 'build/TRIPS_SE/m5.%s --outdir=%s/%s/%s/%s --stats-file\
       stats.txt -re configs/trips_config/%s.py --bench %s --input_set %s &'

# Call os.system() and raise exception if return status is non-zero
def system(cmd):
    try:
        retcode = call(cmd, shell=True)
        if retcode < 0:
            print >>sys.stderr, "Child was terminated by signal", -retcode
            print >>sys.stderr, "When attemping to execute: %s" % cmd
            sys.exit(1)
        elif retcode > 0:
            print >>sys.stderr, "Child returned", retcode
            print >>sys.stderr, "When attemping to execute: %s" % cmd
            sys.exit(1)
    except OSError, e:
        print >>sys.stderr, "Execution failed:", e
        print >>sys.stderr, "When attemping to execute: %s" % cmd
        sys.exit(1)

for bench in benchmarks_to_run:
    outdir = re.sub('(?<=_)\w+','',bench)
    outdir = re.sub('_','',outdir)
    print outdir
    system(cmd % (options.variant,output_base_dir, 
        options.input_set, outdir, options.configuration, 
        options.configuration, bench, options.input_set))

sys.exit(0)

